public class MySQL {

    public static void main(String args[]) {
        test();
    }


    public static void test() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            java.sql.Connection conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/agenda?user=root&password=");


            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rs = stmt.executeQuery("select * from contactos");

            while (rs.next()) {
                System.out.println(rs.getString("nombre_razon"));
            }

            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void test2() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            java.sql.Connection conn = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/agenda?user=root&password=");


            java.sql.Statement stmt = conn.createStatement();            
            
            java.sql.PreparedStatement pstmt = conn.prepareStatement("INSERT INTO contactos (tipo,nombre_razon) values (?,?)",com.mysql.jdbc.Statement.RETURN_GENERATED_KEYS);
            pstmt.setString(1,"E");
            pstmt.setString(2,"Fravega");            
            pstmt.executeUpdate();
            int id = -1;
            java.sql.ResultSet rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                id = rs.getInt(1);
                System.out.println(id);                
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
}
